<?php
/* 
 *  The MIT License
 * 
 *  Copyright 2011 http://
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

/**
 * Classe per la manipolazione delle intestazioni e del protocollo http
 *
 * @author Fabrizio Filieri
 */
class Http
{
	public static $CONTENT_TYPE_APPLICATION_JSON					= "application/json";
	public static $CONTENT_TYPE_APPLICATION_JAVASCRIPT				= "application/javascript";
	public static $CONTENT_TYPE_APPLICATION_OCTET_STREAM			= "application/octet-stream";
	public static $CONTENT_TYPE_APPLICATION_OGG						= "application/ogg";
	public static $CONTENT_TYPE_APPLICATION_PDF						= "application/pdf";
	public static $CONTENT_TYPE_APPLICATION_POSTSCRIPT				= "application/postscript";
	public static $CONTENT_TYPE_APPLICATION_SOAPXML					= "application/soap+xml";
	public static $CONTENT_TYPE_APPLICATION_ZIP						= "application/zip";
	
	public static $CONTENT_TYPE_AUDIO_BASIC							= "audio/basic";
	public static $CONTENT_TYPE_AUDIO_MP4							= "audio/mp4";
	public static $CONTENT_TYPE_AUDIO_MPEG							= "audio/mpeg";
	public static $CONTENT_TYPE_AUDIO_OGG							= "audio/ogg";
	public static $CONTENT_TYPE_AUDIO_VORBIS						= "audio/vorbis";
	public static $CONTENT_TYPE_AUDIO_XMSWMA						= "audio/x-ms-wma";
	public static $CONTENT_TYPE_AUDIO_XMSWAX						= "audio/x-ms-wax";
	public static $CONTENT_TYPE_AUDIO_REALAUDIO						= "audio/vnd.rn-realaudio";
	public static $CONTENT_TYPE_AUDIO_WAVE							= "audio/vnd.wave";
	
	public static $CONTENT_TYPE_IMAGE_GIF							= "image/gif";
	public static $CONTENT_TYPE_IMAGE_JPEG							= "image/jpeg";
	public static $CONTENT_TYPE_IMAGE_PNG							= "image/png";
	public static $CONTENT_TYPE_IMAGE_SVG							= "image/svg+xml";
	public static $CONTENT_TYPE_IMAGE_TIFF							= "image/tiff";
	public static $CONTENT_TYPE_IMAGE_VND							= "image/vnd.microsoft.icon";

	public static $CONTENT_TYPE_TEXT_CMD							= "text/cmd";
	public static $CONTENT_TYPE_TEXT_CSS							= "text/css";
	public static $CONTENT_TYPE_TEXT_CSV							= "text/csv";
	public static $CONTENT_TYPE_TEXT_HTML							= "text/html";
	public static $CONTENT_TYPE_TEXT_JAVASCRIPT						= "text/javascript";
	public static $CONTENT_TYPE_TEXT_PLAIN							= "text/plain";
	public static $CONTENT_TYPE_TEXT_XML							= "text/xml";

	public static $CONTENT_TYPE_VIDEO_MPEG							= "video/mpeg";
	public static $CONTENT_TYPE_VIDEO_MP4							= "video/mp4";
	public static $CONTENT_TYPE_VIDEO_OGG							= "video/ogg";
	public static $CONTENT_TYPE_VIDEO_QUICKTIME						= "video/quicktime";
	public static $CONTENT_TYPE_VIDEO_WEBM							= "video/webm";
	public static $CONTENT_TYPE_VIDEO_WMV							= "video/x-ms-wmv";

	public static $STATUS_200_OK									= 200;

	public static $STATUS_301_MOVED_PERMANENTLY						= 301;
	public static $STATUS_302_FOUND									= 302;
	public static $STATUS_304_NOT_MODIFIED							= 304;
	public static $STATUS_307_TEMPORARY_REDIRECT					= 307;

	public static $STATUS_400_BAD_REQUEST							= 400;
	public static $STATUS_401_UNAUTHORIZED							= 401;
	public static $STATUS_403_FORBIDDEN								= 403;
	public static $STATUS_404_NOT_FOUND								= 404;
	public static $STATUS_405_METHOD_NOT_ALLOWED					= 405;
	public static $STATUS_406_NOT_ACCEPTABLE						= 406;
	public static $STATUS_408_REQUEST_TIMEOUT						= 408;

	public static $STATUS_500_INTERNAL_SERVER_ERROR					= 500;
	public static $STATUS_501_NOT_IMPLEMENTED						= 501;
	public static $STATUS_502_BAD_GATEWAY							= 502;
	public static $STATUS_503_SERVICE_UNAVAILABLE					= 503;

	private static $MSG_STATUS_200_OK								= "HTTP/1.1 200 OK";

	private static $MSG_STATUS_301_MOVED_PERMANENTLY				= "HTTP/1.1 301 Moved Permanently";
	private static $MSG_STATUS_302_FOUND							= "HTTP/1.1 302 Found";
	private static $MSG_STATUS_304_NOT_MODIFIED						= "HTTP/1.1 304 Not Modified";
	private static $MSG_STATUS_307_TEMPORARY_REDIRECT				= "HTTP/1.1 307 Temporary Redirect";

	private static $MSG_STATUS_400_BAD_REQUEST						= "HTTP/1.1 400 Bad Request";
	private static $MSG_STATUS_401_UNAUTHORIZED						= "HTTP/1.1 401 Unauthorized";
	private static $MSG_STATUS_403_FORBIDDEN						= "HTTP/1.1 403 Forbidden";
	private static $MSG_STATUS_404_NOT_FOUND						= "HTTP/1.1 404 Not Found";
	private static $MSG_STATUS_405_METHOD_NOT_ALLOWED				= "HTTP/1.1 405 Method Not Allowed";
	private static $MSG_STATUS_406_NOT_ACCEPTABLE					= "HTTP/1.1 406 Not Acceptable";
	private static $MSG_STATUS_408_REQUEST_TIMEOUT					= "HTTP/1.1 408 Request Timeout";

	private static $MSG_STATUS_500_INTERNAL_SERVER_ERROR			= "HTTP/1.1 500 Internal Server Error";
	private static $MSG_STATUS_501_NOT_IMPLEMENTED					= "HTTP/1.1 501 Not Implemented";
	private static $MSG_STATUS_502_BAD_GATEWAY						= "HTTP/1.1 502 Bad Gateway";
	private static $MSG_STATUS_503_SERVICE_UNAVAILABLE				= "HTTP/1.1 503 Service Unavailable";

	/**
	 * Imposta lo stato http. Si possono usare le costanti definite statiche all'interno di questa classe
	 *
	 * @param int $code il codice dello stato http.
	 */
	public static function httpStatus($code)
	{
		switch ($code)
		{
			case self::$STATUS_200_OK:
				header(self::$MSG_STATUS_200_OK);
				break;

			case self::$STATUS_301_MOVED_PERMANENTLY:
				header(self::$MSG_STATUS_301_MOVED_PERMANENTLY);
				break;

			case self::$STATUS_302_FOUND:
				header(self::$MSG_STATUS_302_FOUND);
				break;

			case self::$STATUS_304_NOT_MODIFIED:
				header(self::$MSG_STATUS_304_NOT_MODIFIED);
				break;

			case self::$STATUS_307_TEMPORARY_REDIRECT:
				header(self::$MSG_STATUS_307_TEMPORARY_REDIRECT);
				break;

			case self::$STATUS_400_BAD_REQUEST:
				header(self::$MSG_STATUS_400_BAD_REQUEST);
				break;

			case self::$STATUS_401_UNAUTHORIZED:
				header(self::$MSG_STATUS_401_UNAUTHORIZED);
				break;

			case self::$STATUS_403_FORBIDDEN:
				header(self::$MSG_STATUS_403_FORBIDDEN);
				break;

			case self::$STATUS_404_NOT_FOUND:
				header(self::$MSG_STATUS_404_NOT_FOUND);
				break;

			case self::$STATUS_405_METHOD_NOT_ALLOWED:
				header(self::$MSG_STATUS_405_METHOD_NOT_ALLOWED);
				break;

			case self::$STATUS_406_NOT_ACCEPTABLE:
				header(self::$MSG_STATUS_406_NOT_ACCEPTABLE);
				break;

			case self::$STATUS_408_REQUEST_TIMEOUT:
				header(self::$MSG_STATUS_408_REQUEST_TIMEOUT);
				break;

			case self::$STATUS_500_INTERNAL_SERVER_ERROR:
				header(self::$MSG_STATUS_500_INTERNAL_SERVER_ERROR);
				break;

			case self::$STATUS_501_NOT_IMPLEMENTED:
				header(self::$MSG_STATUS_501_NOT_IMPLEMENTED);
				break;

			case self::$STATUS_502_BAD_GATEWAY:
				header(self::$MSG_STATUS_502_BAD_GATEWAY);
				break;

			case self::$STATUS_503_SERVICE_UNAVAILABLE:
				header(self::$MSG_STATUS_503_SERVICE_UNAVAILABLE);
				break;
		}
	}

	/**
	 * Imposta il content type. Sono impostate alcune variabili con i content type più comuni come
	 * variabili statiche della classe.
	 *
	 * @param string $content_type il content type
	 */
	public static function setContentType($content_type)
	{
		header('Content-Type: '.$content_type);
	}

	/**
	 * Mostra la pagina 404 del framework.
	 *
	 */
	public static function show404()
	{
		global $bxd;

		Http::httpStatus(Http::$STATUS_404_NOT_FOUND);
		BXD::getInstance()->replaceContent(BXD_PAGE_NOTFOUND);
		include_once BXD_INCLUDEPATH_SITE . BXDConfig::$LAYOUT_FOLDER . '/' . BXD_LAYOUT_ERROR . '.php';
		exit();
	}


}
?>
